package com.example.travelservlet.commentEntertainment.controller;

import com.example.travelservlet.commentEntertainment.service.CommentService;
import com.example.travelservlet.entity.Comment;
import com.example.travelservlet.entity.CommentDto;
import com.example.travelservlet.user.domain.Result;
import com.example.travelservlet.user.domain.UserResult;
import com.example.travelservlet.utils.ResultUtil;
import com.example.travelservlet.utils.ResultUtilPlus1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.UUID;

/**
 * @author hxy
 * @date 2023/5/16 9:04
 */
@RestController
@RequestMapping("/commentController")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @GetMapping("/insertComment/{entertainmentId}/{content}/{userId}")
    public Integer insertComment(@PathVariable String entertainmentId,@PathVariable String content,@PathVariable String userId){
        Comment comment = new Comment();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        TimeZone timeZone = TimeZone.getTimeZone("GMT+8");
        dateFormat.setTimeZone(timeZone);
        comment.setId(UUID.randomUUID().toString());
        comment.setEntertainmentId(entertainmentId);
        comment.setContent(content);
        comment.setCreateOn(dateFormat.format(new Date()));
        comment.setUserId(userId);
        return commentService.insertComment(comment);
    }

    @GetMapping("/findCommentByEntertainmentId/{entertainmentId}")
    public Result<List<CommentDto>> findCommentByEntertainmentId(@PathVariable String entertainmentId){
        List<CommentDto> commentDtoList = commentService.findCommentByEntertainmentId(entertainmentId);
        return ResultUtil.success(commentDtoList);
    }

    @GetMapping("/deleteCommentById/{id}")
    public void deleteCommentById(@PathVariable String id){
        commentService.deleteCommentById(id);
    }

    @GetMapping("selectByKeyWord/{key}")
    public UserResult<List<Comment>> findCommentByKeyWord(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                                          @RequestParam(name = "size", required = false, defaultValue = "10") Integer size,@PathVariable String key){
        List<Comment> pageRequest = commentService.findCommentByKeyWord1(key,page,size);
        List<Comment> comments = commentService.findCommentByKeyWord(key);
        return ResultUtilPlus1.success(pageRequest,comments.size());
    }

    @RequestMapping("/findAllEntertainmentComments")
    @ResponseBody
    public UserResult<List<Comment>> findAllEntertainmentComments(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                                                  @RequestParam(name = "size", required = false, defaultValue = "10") Integer size) {
        List<Comment> commentList = commentService.findAllEntertainmentComments();
        List<Comment> comments = commentService.findAllEntertainmentComments1(page,size);
        return ResultUtilPlus1.success(comments,commentList.size());
    }

    @GetMapping("/selectByEntertainment/{key}")
    public UserResult<List<Comment>> selectByEntertainment(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                                           @RequestParam(name = "size", required = false, defaultValue = "10") Integer size,@PathVariable String key){
        List<Comment> comments = commentService.selectByEntertainment(key);
        List<Comment> pageRequest = commentService.selectByEntertainment1(key,page,size);
        return ResultUtilPlus1.success(pageRequest,comments.size());
    }

}
